package com.znsb.msfq.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.znsb.msfq.BaseActivity;
import com.znsb.msfq.R;
import com.znsb.msfq.app.ZnsbApp;
import com.znsb.msfq.utils.HttpUtils;
import com.znsb.msfq.utils.LogUtils;
import com.znsb.msfq.utils.ResponseUtils;
import com.znsb.msfq.utils.ToastUtils;
import com.znsb.msfq.utils.UrlUtils;
import com.znsb.msfq.utils.ValidateUtils;
import com.znsb.msfq.view.MyTBView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @name： 孙科技
 * @phone： 13308334117
 * @createTime： 2017/2/16
 * @modifyTime： 2017/2/16
 * @explain：
 */
public class ForgetPwdActivity extends BaseActivity {
    @Bind(R.id.bar_img_back)
    ImageView barImgBack;
    @Bind(R.id.bar_tv_title)
    TextView barTvTitle;
    @Bind(R.id.bar_tv_other)
    TextView barTvOther;
    @Bind(R.id.forgetpwd_tv_phone)
    TextView forgetpwdTvPhone;
    @Bind(R.id.forgetpwd_edit_phone)
    EditText forgetpwdEditPhone;
    @Bind(R.id.forgetpwd_button_timer)
    MyTBView forgetpwdButtonTimer;
    @Bind(R.id.forgetpwd_tv_code)
    TextView forgetpwdTvCode;
    @Bind(R.id.forgetpwd_edit_code)
    EditText forgetpwdEditCode;
    @Bind(R.id.forgetpwd_tv_pwd)
    TextView forgetpwdTvPwd;
    @Bind(R.id.forgetpwd_edit_pwd)
    EditText forgetpwdEditPwd;
    @Bind(R.id.forgetpwd_tv_agpwd)
    TextView forgetpwdTvAgpwd;
    @Bind(R.id.forgetpwd_edit_agpwd)
    EditText forgetpwdEditAgpwd;
    @Bind(R.id.forgetpwd_tv_btn)
    TextView forgetpwdTvBtn;

    @Override
    protected int getlayoutView() {
        return R.layout.act_forget_pwd;
    }

    @Override
    protected void initView(View view) {
        barTvTitle.setText("忘记密码");
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void clickListener(View view) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.bar_img_back, R.id.forgetpwd_button_timer, R.id.forgetpwd_tv_btn})
    public void onClick(View view) {
        String phone = forgetpwdEditPhone.getText().toString().trim();
        switch (view.getId()) {
            case R.id.bar_img_back:
                finish();
                break;
            case R.id.forgetpwd_button_timer:
                if (ValidateUtils.isMobile(phone)) {
                    getForgetpwdCode(phone);
                    forgetpwdButtonTimer.start();
                }
                break;
            case R.id.forgetpwd_tv_btn:
                String code = forgetpwdEditCode.getText().toString().trim();
                String pwd = forgetpwdEditPwd.getText().toString().trim();
                String agpwd = forgetpwdEditAgpwd.getText().toString().trim();
                if (ValidateUtils.isForgetPwd(phone, code, pwd, agpwd))
                    setForgetpwd(phone, code, pwd, agpwd);
                break;
        }
    }

    /**
     * 获取忘记验证码
     *
     * @param phone
     */
    private void getForgetpwdCode(String phone) {
        Map<String, Object> maps = new HashMap<>();
        maps.put("phone", phone);
        HttpUtils.getInstance(this).postAsnyRequest(UrlUtils.MSFQ_URL_FORGETPWDCODE, HttpUtils.getMaps(maps),
                new ResponseUtils() {
                    @Override
                    public void onSucceed(String code, JSONObject request) {
                        LogUtils.i("TAG:", "forgetpwd_code=" + request);
                    }

                    @Override
                    public void onFailure(String result) {
                    }
                });
    }

    /**
     * 忘记密码
     */
    private void setForgetpwd(String phone, String code, String pwd, String agPwd) {
        Map<String, Object> maps = new HashMap<>();
        maps.put("username", phone);
        maps.put("password", pwd);
        maps.put("affirmPassword", agPwd);
        maps.put("resetCode", code);
        HttpUtils.getInstance(this).postAsnyRequest(UrlUtils.MSFQ_URL_FORGETPWD, HttpUtils.getMaps(maps),
                new ResponseUtils() {
                    @Override
                    public void onSucceed(String code, JSONObject request) {
                        LogUtils.i("TAG:", "forgetpwd=" + request);
                        finish();
                    }

                    @Override
                    public void onFailure(String result) {
                    }
                });
    }
}
